package com.woyou.ui.activity.orderdetail;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.citaq.ideliver.R;

@SuppressLint("NewApi")
public class TimeLineLayout extends LinearLayout {
	private float density;

	public TimeLineLayout(Context context) {
		super(context);
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		density = dm.density;
	}

	public TimeLineLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		density = dm.density;
	}

	public TimeLineLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		density = dm.density;
	}

	public void setAdapter(OrderDetailAdapter b) {
		this.setOrientation(LinearLayout.VERTICAL);
		removeAllViews();
		View vs[] = b.getView();
		boolean flag = true;
		for (int i = 0; i < b.getCount() + vs.length; i++) {
			LinearLayout layout = new LinearLayout(this.getContext());
			layout.setOrientation(LinearLayout.HORIZONTAL);
			TimeLineView headView = null;
			View childView = null;
			LinearLayout diverLayout = new LinearLayout(this.getContext());
			diverLayout.setOrientation(LinearLayout.VERTICAL);
			if (i < b.getCount()) {
				childView = b.getLog(i);
			} else {
				childView = vs[i - b.getCount()];
			}
			// childView.setMinimumHeight((int)
			// (getResources().getDimension(R.dimen.dp45) * density * 1.5));
			diverLayout.addView(childView);
			View diver = new View(this.getContext());
			if (i < b.getCount() + vs.length - 1) {
				diver.setBackgroundColor(0xffc8c8c8);
			}
			diverLayout.addView(diver, new LayoutParams(LayoutParams.MATCH_PARENT,
					(int) Math.floor(density * getResources().getDimension(R.dimen.dp1))));
			if (b.getCount() + vs.length == 1) {
				headView = new TimeLineView(this.getContext(), 0);
			} else if (i == 0) {
				headView = new TimeLineView(this.getContext(), 1);
			} else if (i == b.getCount() + vs.length - 1) {
				if (childView.getId() == R.id.button) {
					headView = new TimeLineView(this.getContext(), 6);
					if (flag) {
						((TimeLineView) ((ViewGroup) getChildAt(i-1)).getChildAt(0)).setType(4);
						flag = false;
					}
				} else {
					headView = new TimeLineView(this.getContext(), 3);
				}
			} else {
				if (childView.getId() == R.id.button) {
					headView = new TimeLineView(this.getContext(), 5);
					if (flag) {
						((TimeLineView) ((ViewGroup) getChildAt(i-1)).getChildAt(0)).setType(4);
						flag = false;
					}
				} else {
					headView = new TimeLineView(this.getContext(), 2);
				}
			}
			layout.addView(headView,
					new LayoutParams((int) (getResources().getDimension(R.dimen.dp150)), LayoutParams.MATCH_PARENT));
			diverLayout.setPadding(0, 0, (int) (getResources().getDimension(R.dimen.dp70)), 0);
			layout.addView(diverLayout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			this.addView(layout);
		}
	}

	private class TimeLineView extends View {
		private int type;
		private Paint paint;
		private Paint paintStroke;
		private Paint graypaint;
		private Paint graypaintStroke;

		public TimeLineView(Context context, int type) {
			super(context);
			this.type = type;
			paint = new Paint();
			paint.setStrokeWidth(density);
			paint.setColor(0xff0081ff);
			paint.setFlags(Paint.ANTI_ALIAS_FLAG);
			paintStroke = new Paint();
			paintStroke.setColor(0xffb3d9fe);
			paintStroke.setFlags(Paint.ANTI_ALIAS_FLAG);
			graypaintStroke = new Paint();
			graypaint = new Paint();
			graypaintStroke.setFlags(Paint.ANTI_ALIAS_FLAG);
			graypaint.setFlags(Paint.ANTI_ALIAS_FLAG);
			graypaintStroke.setColor(0xffbcbcbc);
			graypaint.setColor(0xffbcbcbc);
		}

		public void setType(int i) {
			type = i;
		}

		@Override
		protected void onDraw(Canvas canvas) {
			if (type == 0) {
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5 + 2 * density, paintStroke);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5, paint);
			}
			if (type == 1) {
				canvas.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight(), paint);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5 + 2 * density, paintStroke);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5, paint);
			}
			if (type == 2) {
				canvas.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight(), paint);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5 + 2 * density, paintStroke);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5, paint);
			}
			if (type == 3) {
				canvas.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight() / 2, paint);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5 + 2 * density, paintStroke);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5, paint);
			}
			if (type == 4) {
				canvas.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight() / 2, paint);
				canvas.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight(), graypaint);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5 + 2 * density, paintStroke);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5, paint);
			}
			if (type == 5) {
				canvas.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight(), graypaint);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5 + 2 * density, graypaintStroke);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5, graypaint);
			}
			if (type == 6) {
				canvas.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight() / 2, graypaint);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5 + 2 * density, graypaintStroke);
				canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5, graypaint);
			}

		}
	}

}
